// pages/huiyuan/huiyuan.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    isShow: true
  },
  redictToAddress: function() {
    wx.navigateTo({
      url: '/pages/yuding/dizhi/dizhi',
    });
  },
  comment: function () {
    wx.navigateTo({
      url: '/pages/liuyan/liuyan',
    })
  },
  customerService: function() {

  },
  customerPhone: function() {
    wx.makePhoneCall({
      phoneNumber: app.globalData.customerPhone //仅为示例，并非真实的电话号码
    });
  },
  // redictToAgreement: function() {
  //   wx.redirectTo({
  //     url: '/pages/yuding/xieyi/xieyi',
  //   })
  // },
  handleGetUserInfo(data) {
    let that = this;
    wx.getUserInfo({
      success(res) {
        console.log(res.userInfo);
        that.setData({
          userInfo:res.userInfo,
        });
        that.login();
      }
    });


  },
  login: function() {
    wx.showToast({
      title: '正在登录',
      icon: 'loading'
    });
    let that = this;
    wx.login({
      success(res) {
        wx.request({
          url: app.globalData.baseUrl + "/user/login/wechat?code=" + res.code,
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          data:{
            name: that.data.userInfo["nickName"],
            avatarUrl: that.data.userInfo.avatarUrl
          },
          success(res) {
            wx.hideToast();
            console.log("loginRes", res);
            if(res.data.id) {
              let cookieStr = res.cookies[0];
              let startIndex = cookieStr.indexOf("=");
              let endIndex = cookieStr.indexOf(";");
              let userInfo = {};
              userInfo.sessionId = cookieStr.substring(startIndex + 1, endIndex);
              userInfo.userId = res.data.id;
              userInfo.openId = res.data.wechatId;
              userInfo.nickName = res.data.name;
              userInfo.avatarUrl = res.data.avatarUrl
              wx.setStorage({
                key: "userInfo",
                data: userInfo,
              });
              that.setData({
                isShow: false
              });
              wx.showToast({
                title: '登录成功！',
                icon: "success",
                duration: 1000
              });
            } else {
              wx.showToast({
                title: '服务器异常！',
                icon: "loading",
                duration: 2000
              });
            }
          },
          fail(err) {
            wx.showToast({
              title: '服务器异常！',
              icon: "loading",
              duration: 2000
            });
            console.log("loginErr", err);
          }
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    if (wx.getStorageSync("userInfo")) {
      that.setData({
        userInfo: wx.getStorageSync("userInfo")
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

})